package cn.edu.seu.ssny.controller.web;

import cn.edu.seu.ssny.common.*;
import cn.edu.seu.ssny.common.utils.DateUtils;
import cn.edu.seu.ssny.controller.BaseController;
import cn.edu.seu.ssny.enums.ResultCodeEnum;
import cn.edu.seu.ssny.exception.SsnyException;
import cn.edu.seu.ssny.service.IDeviceDataStdService;
import cn.edu.seu.ssny.service.IStatisticsConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 首页图表数据
 */
@RestController
@RequestMapping("/index/data")
public class IndexDataController extends BaseController {
    @Autowired
    private IDeviceDataStdService deviceDataStdService;

    @Autowired
    private IStatisticsConfigService statisticsDeviceService;

    /**
     * 功能描述:
     * <获得系统能耗占比
     * <返回设备类别计算设备能耗及占比(所有项目)
     *
     * @return
     */
    @GetMapping("/syspower")
    public R getSystemPower() {
        List<String> equipmetTypes = BusinessCollections.powerCostTypes;
        List<Double> powerCosts = deviceDataStdService.getPowerCostByEquipmentTypes(equipmetTypes);
        Map<String, Object> data = new HashMap<>();
        data.put("equipments", equipmetTypes);
        data.put("powercosts", powerCosts);
        return R.ok().data(data);
    }

    /**
     * 功能描述
     * <获得供冷能耗/供暖能耗(所有项目)
     * <最近7天
     *
     * @param mode 0:供冷能耗 1:供暖能耗
     * @return
     */
    @GetMapping("/power")
    public R getModePower(@RequestParam("mode") int mode) {
        List<Date> timeList = DateUtils.getDateList(DateUtils.MODE_DAY, 10);
        List<String> equipmentTypes = null;
        if (mode == Constants.CoolingMode) {
            equipmentTypes = BusinessCollections.powerCostColdTypes;
        } else if (mode == Constants.HeatingMode) {
            equipmentTypes = BusinessCollections.powerCostHotTypes;
        } else {
            throw new SsnyException(ResultCodeEnum.ERROR_MODE);
        }
        timeList.add(new Date());
        List<Double> powerCosts = deviceDataStdService.getPowerCostByDaysAndEquipmentTypes(timeList, equipmentTypes);
        timeList.remove(timeList.size() - 1);
        List<String> dates = transDateListToDateString(timeList);
        Map<String, Object> data = new HashMap();
        data.put("dates", dates);
        data.put("powercosts", powerCosts);
        return R.ok().data(data);
    }

    /**
     * 功能描述:
     * <获取制冷能效或者供暖能效，不区分项目
     *
     * @param mode 0:制冷能效 1:供暖能效
     * @return
     */
    @GetMapping("/scop")
    public R getScop(@RequestParam("mode") int mode) {
        List<Date> timeList = DateUtils.getDateList(DateUtils.MODE_DAY, 10);
        List<String> powercostEquipements = null;
        List<String> eneryEquipmentTypes = null;
        if (mode == Constants.CoolingMode) {
            eneryEquipmentTypes = BusinessCollections.providerColdTypes;
            powercostEquipements = BusinessCollections.powerCostColdTypes;
        } else if (mode == Constants.HeatingMode) {
            eneryEquipmentTypes = BusinessCollections.providerHotTypes;
            powercostEquipements = BusinessCollections.powerCostHotTypes;
        } else {
            throw new SsnyException(ResultCodeEnum.ERROR_MODE);
        }
        timeList.add(new Date());
        List<Double> scops = deviceDataStdService.getSCOP(timeList, powercostEquipements, eneryEquipmentTypes, mode, DateUtils.MODE_DAY);
        timeList.remove(timeList.size() - 1);
        List<String> dates = transDateListToDateString(timeList);
        Map<String, Object> data = new HashMap();
        data.put("dates", dates);
        data.put("scops", scops);
        return R.ok().data(data);
    }


}
